package com.kayang.feishu.controller;


import com.kayang.common.HttpMsg;
import com.kayang.common.util.MapUtil;
import com.kayang.feishu.config.FeiShuConfig;
import com.kayang.feishu.service.AccessTokenService;
import com.kayang.feishu.pojo.FeiShuContext;
import com.kayang.feishu.pojo.msg.TextMsg;
import com.kayang.feishu.service.MessageService;
import com.kayang.nadapt.util.Convert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;


@RestController
@RequestMapping(value = "/message")
public class MessageController extends BaseController {

    @Autowired
    AccessTokenService tokenService;

    @Autowired
    FeiShuConfig feiShuConfig;

    @Autowired
    MessageService msgService;

    @PostMapping("/sendText")
    public ResponseEntity sendText(@Valid @RequestBody TextMsg textMsg){
        String url = feiShuConfig.getPath() + "/open-apis/im/v1/messages?receive_id_type={receive_id_type}";
        Map param = MapUtil.toMap(textMsg);
        url = url.replace("{receive_id_type}", Convert.toString(param.get("receive_id_type")));
        param.remove("receive_id_type");
        FeiShuContext context = new FeiShuContext();
        context.setUrl(url);
        context.setMethod("POST");
        context.setBody(param);
        Map header = new HashMap();
        header.put("Authorization", "Bearer " + tokenService.getAccessTokenStr(feiShuConfig.getAppKey()));
        header.put("Content-Type", "application/json; charset=utf-8");
        Map ret = msgService.send(param, context, header);
        HttpMsg httpMsg = new HttpMsg();
        httpMsg.setMsgId(0);
        httpMsg.setMsg("创建成功");
        httpMsg.setResult(ret);
        return ResponseEntity.ok(httpMsg);
    }

//    @PostMapping("/sendImage")
//    public ResponseEntity sendImage( @RequestParam("receive_id_type") String receive_id_type,
//                                     @RequestParam("receive_id") String receive_id,
//                                     @RequestPart("file") MultipartFile file){
//        Map header = new HashMap();
//        header.put("Authorization", "Bearer " + tokenService.getAccessTokenStr(feiShuConfig.getAppKey()));
//        header.put("Content-Type", "application/json; charset=utf-8");
//
//        String imageUrl = feiShuConfig.getPath() + "/open-apis/im/v1/images";
//
//
//
//
//
//        String msgurl = feiShuConfig.getPath() + "/open-apis/im/v1/message?receive_id_type=open_id";
//        Map param = new HashMap();
//        param.put("receive_id_type", receive_id_type);
//        param.put("receive_id", receive_id);
//        param.put("content", receive_id_type);
//        param.put("msg_type", "image");
//
//
//        msgurl = msgurl.replace("{client_token}", Convert.toString(param.get("client_token")))
//                .replace("{open_department_id}", Convert.toString(param.get("open_department_id")))
//                .replace("{user_id_type}", Convert.toString(param.get("user_id_type")));
//        param.remove("client_token");
//        param.remove("open_department_id");
//        param.remove("user_id_type");
//        FeiShuContext context = new FeiShuContext();
//        context.setUrl(msgurl);
//        context.setMethod("POST");
//        context.setBody(param);
//
//        Map ret = msgService.send(param, context, header);
//        HttpMsg httpMsg = new HttpMsg();
//        httpMsg.setMsgId(0);
//        httpMsg.setMsg("创建成功");
//        httpMsg.setResult(ret);
//        return ResponseEntity.ok(httpMsg);
//    }




}
